rm(list = ls())

library(data.table)
library(estimatr)
library(ggplot2)

panel_all <- get(load('./data/panel_month_dummies_all_2020_months.RData'))

# Figure C3 (a)
# coefficients post-covid
mon <- seq(as.Date("2020-04-01"), by='month', length.out=9)

effectp <- vector(mode = 'list', length = length(mon))
ci_lowp <- vector(mode = 'list', length = length(mon))
ci_highp <- vector(mode = 'list', length = length(mon))
monthp <- vector(mode = 'list', length = length(mon))

for (m in seq_along(mon)) {
  print(mon[m])
  ppost <- panel_all[(month<'2020-01-01' | month==mon[m])]
  ppost[,post:=ifelse(month==mon[m],1,0)]
  ppost[,treat:=post*excess_deaths_jan_d]
  
  out <- lm_robust(hc_pc_asians ~ treat, data=ppost, se_type='stata',
                   clusters = panelvar, fixed_effects = ~ panelvar + month)
  
  effectp[m] <- out$coefficients
  ci_lowp[m] <- out$conf.low
  ci_highp[m] <- out$conf.high
  monthp[[m]] <- mon[m]
  
}

coeffs_post <- data.frame(effect=unlist(effectp), ci_low=unlist(ci_lowp), ci_high=unlist(ci_highp), month=do.call("c", monthp))

load('./ouput/coeff_deaths_jan_backup.RData')
coeffs <- rbind(coeffs_post, coeffs)
save(coeffs, file='./output/coeff_deaths_jan_backup_all_2020_months.RData')

# Figure C3 (b)
effectp <- vector(mode = 'list', length = length(mon))
ci_lowp <- vector(mode = 'list', length = length(mon))
ci_highp <- vector(mode = 'list', length = length(mon))
monthp <- vector(mode = 'list', length = length(mon))

for (m in seq_along(mon)) {
  print(mon[m])
  ppost <- panel_all[(month<'2020-01-01' | month==mon[m])]
  ppost[,post:=ifelse(month==mon[m],1,0)]
  ppost[,treat:=post*losing_income_d]
  
  out <- lm_robust(hc_pc_asians ~ treat, data=ppost, se_type='stata',
                   clusters = panelvar, fixed_effects = ~ panelvar + month)
  
  effectp[m] <- out$coefficients
  ci_lowp[m] <- out$conf.low
  ci_highp[m] <- out$conf.high
  monthp[[m]] <- mon[m]
  
}

coeffs_post <- data.frame(effect=unlist(effectp), ci_low=unlist(ci_lowp), ci_high=unlist(ci_highp), month=do.call("c", monthp))

load('./output/coeff_economic_loss_backup.RData')
coeffs <- rbind(coeffs, coeffs_post)
save(coeffs, file='./output/coeff_economic_loss_backup_all_2020_months.RData')